﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 挂载此脚本以防止一级子节点的所有粒子特效受Time.timeScale影响
/// </summary>
public class ParticleUnscaledTime : MonoBehaviour
{
    private float timeScale = 1;

    private List<ParticleSystem> particleSystems = new List<ParticleSystem>();

    private void Start()
    {
        particleSystems.Clear();
        for (int i = 0; i < transform.childCount; i++)
        {
            var childParticle = transform.GetChild(i).GetComponent<ParticleSystem>();
            if (childParticle != null)
            {
                particleSystems.Add(childParticle);
            }
        }
    }

    void Update()
    {
        if (timeScale != Time.timeScale)
        {
            foreach (var particleSys in particleSystems)
            {
                var main = particleSys.main;
                if (timeScale != 0)
                {
                    main.simulationSpeed *= timeScale / Time.timeScale;
                }
                else
                {
                    main.simulationSpeed = 1;
                }
            }
            timeScale = Time.timeScale;
        }
    }
}
